package com.tom.nio;


import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author zx
 * @date 2024年09月30日 14:56
 */
public class TestNio1 {
    private static final String PAPTH = "C:\\Users\\86188\\Desktop\\springboot3.x-technology-tools\\netty\\netty-basic-01\\src\\main\\java\\com\\tom\\nio\\data.txt";

    public static void main(String[] args) {
        FileChannel channel = null;
        try {
            channel = new FileInputStream(PAPTH).getChannel();

            ByteBuffer buffer = ByteBuffer.allocate(10);
            while (true) {
                int read = channel.read(buffer);
                if (read == -1) break;

                buffer.flip();
                while (buffer.hasRemaining()) {
                    byte b = buffer.get();
                    System.out.println("(char)b=" + (char) b);
                }

                buffer.clear();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (channel != null) {
                try {
                    channel.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
